/**
 * Created by huzhihui on 2016/11/22.
 */
/*弹框变量*/
var layerLoad;
var layerNumber;
$(function(){
    initProductTypeList();
    initProductType();
});
/**
 * 商品类型--列表查询
 */
function initProductTypeList(){
    $.ajax({
        url : $api.role.selectAllRoles,
        type : "GET",
        dataType : "JSON",
        contentType: 'application/json',
        beforeSend : function () {
            layerLoad = layer.load();
        },
        success : function(data) {
            if(!$checkRemouteStatus(data)){
                layer.close(layerLoad);
                return;
            }
            layui.use('laytpl', function() {
                var laytpl = layui.laytpl;
                laytpl(sysProductTypeListTpl.innerHTML).render(data, function(html) {
                    $('#sys-order-list-body').html(html)
                });
            });
            layer.close(layerLoad);
        },
        error : function() {
            layer.close(layerLoad);
            layer.msg("网络开小差啦", {icon: 2});
        }
    });
};
/*类型*/
function initProductType(){
    $.ajax({
        url : $api.role.selectFirstGradeMenus,
        type : "GET",
        dataType : "JSON",
        contentType: 'application/json',
        beforeSend : function () {
            layerLoad = layer.load();
        },
        success : function(data) {
            if(!$checkRemouteStatus(data)){
                layer.close(layerLoad);
                return;
            }
            var dataObj=data.msg;
            var orleTxt="";
            $.each(dataObj,function(index,data) {
                orleTxt+="<label class='am-checkbox-inline'><input type='checkbox' value='"+data.id+"'/>"+data.name+"</label>";
            });
            $("#roleBox").html(orleTxt);
            layer.close(layerLoad);
        },
        error : function() {
            layer.close(layerLoad);
            layer.msg("网络开小差啦", {icon: 2});
        }
    });
};
/*编辑*/
var roleId="";
function edit(othis){
    roleId=$(othis).attr("shopId");
    $.ajax({
        url: $api.role.selectRoleById+ "?roleId=" + roleId,
        type: "GET",
        dataType: "JSON",
        contentType : 'application/json',
        beforeSend: function () {
            layerLoad = layer.load();
        },
        success: function (data) {
            layer.close(layerLoad);
            var dataObj=data.msg;
            if(data.code == '111111'){
                layer.msg(data.msg, {icon: 2});
            }else{
                /*数据回显*/
                $("#roleName").val(dataObj.name);
                $("#roleDescription").val(dataObj.description);
                Selected("#roleBox input:checkbox",dataObj.menuIds);
            }
        },
        error: function () {
           layer.msg("网络开小差啦", {icon: 2});
        }
    });
    layerNumber=layer.open({
        type : 1,
        title : '角色编辑',
        area : 'auto',
        maxWidth : '1000px',
        content : $('#system-product-category-update-model'),
        cancel : function(){
            $('#sys-product-type-update-form')[0].reset();
            $('#sysProductImgUrlUpdate').attr('src',$assets.headChooseIcon);
        }
    });
};
/*checked拼接*/
function checkedTxt(domTxt){
    var ranksTxt="";
    $(domTxt).each(function(index,dom){
        if($(dom).is(':checked')){
            ranksTxt+=","+$(dom).val();
        };
    });
    var ranksResult = ranksTxt.substring(1);
    return ranksResult;
};
/*checked回显*/
function Selected(domTxt,dataTxt){
    var productCategoryIds=dataTxt.split(",");
    $(domTxt).each(function(index,dom){
        if(productCategoryIds.indexOf($(dom).val())!== -1){
            $(dom).attr("checked","checked");
        }else{
            $(dom).removeAttr("checked");
        }
    });
};
/*保存*/
$("#save").click(function(){
    var productData={};
    productData["name"]=$("#roleName").val();
    if(roleId!=="新增"){
        productData["id"]=roleId;
    }
    productData["description"]=$("#roleDescription").val();
    productData["menuIds"]=checkedTxt("#roleBox input:checkbox");
    $.ajax({
        url: $api.role.insertOrUpdateRole,
        type: "POST",
        dataType: "JSON",
        contentType : 'application/json',
        data :  JSON.stringify(productData) ,
        beforeSend: function () {
            layerLoad = layer.load();
        },
        success: function (data) {
            layer.close(layerLoad);
            var dataObj=data.msg;
            if(data.code == '111111'){
                layer.msg(data.msg, {icon: 2});
            }else{
                layer.msg(data.msg, {icon: 1});
                initProductTypeList();
                layer.close(layerNumber);
                $("#name").val("");
                $("#roleDescription").val("");
            }
        },
        error: function () {
            layer.msg("网络开小差啦", {icon: 2});
        }
    });

});

/*新增*/
$("#shop-print-add").click(function(){
    $("#roleName").val("");
    $("#roleDescription").val("");
    Selected("#roleBox input:checkbox","");
    roleId="新增";
    layerNumber=layer.open({
        type : 1,
        title : '新增角色',
        area : 'auto',
        maxWidth : '1000px',
        content : $('#system-product-category-update-model'),
        cancel : function(){
            $('#sys-product-type-update-form')[0].reset();
            $('#sysProductImgUrlUpdate').attr('src',$assets.headChooseIcon);
        }
    });
});